/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.messages;

import cz.muni.fi.diplomka.servicelayer.dto.Message;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MessageManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.PlayerManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MessageManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.PlayerManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.sql.Timestamp;
import java.util.Date;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

/**
 *
 * @author Jakub Kutil
 */
public class NewMessagePage extends WebPage {

    private MessageManager messageManager = new MessageManagerImpl();
    private PlayerManager playerManager = new PlayerManagerImpl();

    public NewMessagePage(PageParameters parameters) {
        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));
            add(new MessagePanel("messagePanel", p));

            final RequiredTextField<String> to = new RequiredTextField<>("to", Model.of(""));

            final RequiredTextField<String> subject = new RequiredTextField<>("subject", Model.of(""));

            final RequiredTextField<String> body = new RequiredTextField<>("body", Model.of(""));

            Form<?> form = new Form<Void>("form") {
                @Override
                protected void onSubmit() {
                    if (!(playerManager.findPlayerByLogin(to.getValue()).getId() == null)) {
                        Message message = new Message();
                        message.setIdFrom(id);
                        message.setIdTo(playerManager.findPlayerByLogin(to.getValue()).getId());
                        message.setSubject(subject.getValue());
                        message.setBody(body.getValue());
                        message.setTime(new Timestamp(new Date().getTime()));
                        message.setUnread(true);
                        
                        messageManager.addMessage(message);
                        info("Message has been sent.");
                        to.clearInput();
                        subject.clearInput();
                        body.clearInput();
                    } else {
                        error("Player not found");
                    }
                }
            };

            add(form);
            form.add(to);
            form.add(subject);
            form.add(body);

        }
    }
}
